package com.hsd.znsh.listener;

import com.hsd.znsh.entity.Control;
import com.hsd.znsh.entity.Event;
import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.Sensor;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.enums.ReadStatusEnum;
import com.hsd.znsh.repository.ControlRepository;
import com.hsd.znsh.repository.EventRepository;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.repository.SensorRepository;
import com.hsd.znsh.service.LoraService;
import com.hsd.znsh.service.PushService;
import com.hsd.znsh.utils.DateUtil;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-07-09 13:44:16
 */
public class ControlListenerThread extends Thread {

    public boolean flag = true;

    private ControlRepository controlRepository;

    private HostRepository hostRepository;

    private EventRepository eventRepository;

    private PushService pushService;

    private LoraService loraService;

    public ControlListenerThread(ControlRepository controlRepository, HostRepository hostRepository,
                                 EventRepository eventRepository, PushService pushService, LoraService loraService) {
        this.controlRepository = controlRepository;
        this.hostRepository = hostRepository;
        this.eventRepository = eventRepository;
        this.pushService = pushService;
        this.loraService = loraService;
        this.setDaemon(true);
    }

    @Override
    public void run() {
        while (flag) {
            try {
                List<Control> controlList = controlRepository.findAll();
                for (Control control : controlList) {
                    Date date = loraService.getDeviceLastDate(control.getControlSn());
                    if (date == null) continue;
                    LocalDateTime controlTime = DateUtil.dateToLocalDateTime(date);
                    LocalDateTime nowTime = LocalDateTime.now();
                    if (control.getOnlineStatus() == OnlineStatusEnum.ONLINE.getCode() &&
                            controlTime.plusSeconds(control.getFrequency() * 2 + 5).isBefore(nowTime)) {
                        System.out.println("控制器下线" + DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
                        Host host = hostRepository.findOne(control.getHostId());
                        String content;
                        String title;
                        if (control.getAlias() == null || control.getAlias().equals("")) {
                            content = control.getName() + "(" + control.getControlSn() + ") 下线";
                        } else {
                            content = control.getAlias() + "(" + control.getControlSn() + ") 下线";
                        }
                        if (host.getAlias() == null || host.getAlias().equals("")) {
                            title = host.getHostName();
                        } else {
                            title = host.getAlias();
                        }
                        Event event = new Event();
                        event.setReadStatus(ReadStatusEnum.NOT_READ.getCode());
                        event.setContent(content);
                        event.setDeviceSn(control.getControlSn());
                        event.setEventLevel(1);
                        event.setGatewayName(title);
                        event.setEventType("通知");
                        event.setGatewaySn(host.getHostSn());

                        eventRepository.save(event);
                        pushService.pushToGroup(title, content, host.getHostSn());
                        control.setOnlineStatus(OnlineStatusEnum.NOT_ONLINE.getCode());
                        controlRepository.save(control);
                    } else if (control.getOnlineStatus() == OnlineStatusEnum.NOT_ONLINE.getCode() &&
                            controlTime.plusSeconds(control.getFrequency() * 2).isAfter(nowTime)) {
                        System.out.println("控制器上线" + DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
                        Host host = hostRepository.findOne(control.getHostId());
                        String content;
                        String title;
                        if (control.getAlias() == null || control.getAlias().equals("")) {
                            content = control.getName() + "(" + control.getControlSn() + ") 上线";
                        } else {
                            content = control.getAlias() + "(" + control.getControlSn() + ") 上线";
                        }
                        if (host.getAlias() == null || host.getAlias().equals("")) {
                            title = host.getHostName();
                        } else {
                            title = host.getAlias();
                        }
                        Event event = new Event();
                        event.setReadStatus(ReadStatusEnum.NOT_READ.getCode());
                        event.setContent(content);
                        event.setDeviceSn(control.getControlSn());
                        event.setEventLevel(1);
                        event.setGatewayName(title);
                        event.setEventType("通知");
                        event.setGatewaySn(host.getHostSn());

                        eventRepository.save(event);
                        pushService.pushToGroup(title, content, host.getHostSn());
                        control.setOnlineStatus(OnlineStatusEnum.ONLINE.getCode());
                        controlRepository.save(control);
                    }
                }
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
